package cn.lf.android.lawfirm.undertake.approval;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.zhy.autolayout.AutoRelativeLayout;

import java.util.ArrayList;
import java.util.List;

import cn.lf.android.lawfirm.BaseActivity;
import cn.lf.android.lawfirm.R;
import cn.lf.android.lawfirm.undertake.apply.view.AddLawyerFeeAdapter;
import cn.lf.android.lawfirm.undertake.approval.controler.LawyerFeeInfoController;
import cn.lf.android.lawfirm.undertake.approval.modle.LawyerFee;
import cn.lf.android.lawfirm.undertake.approval.modle.LawyerFeeC;
import cn.lf.android.lawfirm.undertake.approval.modle.LawyerFeeInfo;
import cn.lf.android.lawfirm.utils.timepicker.TimePicker;
import cn.lf.android.lawfirm.view.NoScrollListView;

import static android.R.attr.data;
import static android.icu.text.DateTimePatternGenerator.PatternInfo.OK;
import static cn.lf.android.lawfirm.R.id.baselawyer_fee;
import static cn.lf.android.lawfirm.R.id.eb_tv_right;

public class AddLawyerFeeActivity extends BaseActivity implements LawyerFeeInfoController.FeeInfoCallback {

    /**
     * 返回
     **/
    private ImageButton img_left;
    /**
     * 标题
     **/
    private TextView mtitle;
    private TextView mTvRight;
    private String caseID = "";
    private LawyerFeeInfoController mLawyerFeeInfoController;
    private EditText name, fee, clause;
    private AutoRelativeLayout rl_fee, kuanxiang, lvfee, tiaokuan;
    private TextView time;
    private TimePicker mTimePicker;
    private NoScrollListView lv_list;
    AddLawyerFeeAdapter mAddLawyerFeeAdapter;
    private String from = "";

    @Override
    public Object getContentResView() {
        return R.layout.activity_add_lawyerfee;
    }

    @Override
    public void initView() {
        initTitle();
        from = getIntent().getStringExtra("from");

        LawyerFeeC b = (LawyerFeeC) getIntent().getSerializableExtra("lawyerfee");
        mBean.addAll(b.bean);
        caseID = getIntent().getStringExtra("caseID");
        rl_fee = (AutoRelativeLayout) findViewById(R.id.rl_fee);
        rl_fee.setOnClickListener(this);
        kuanxiang = (AutoRelativeLayout) findViewById(R.id.kuanxiang);
        lvfee = (AutoRelativeLayout) findViewById(R.id.lvfee);
        tiaokuan = (AutoRelativeLayout) findViewById(R.id.tiaokuan);

        if ("ApplyUndertakeInfoActivity".equals(from)) {
            rl_fee.setVisibility(View.GONE);
            kuanxiang.setVisibility(View.GONE);
            lvfee.setVisibility(View.GONE);
            tiaokuan.setVisibility(View.GONE);
            mTvRight.setText("");
        }

        name = (EditText) findViewById(R.id.name);
        fee = (EditText) findViewById(R.id.fee);
        clause = (EditText) findViewById(R.id.clause);
        time = (TextView) findViewById(R.id.time);
        mAddLawyerFeeAdapter = new AddLawyerFeeAdapter(this, mBean,from);
        lv_list = (NoScrollListView) findViewById(R.id.lv_list);
        lv_list.setAdapter(mAddLawyerFeeAdapter);
    }


    private void initTitle() {
        img_left = (ImageButton) findViewById(R.id.img_left);
        img_left.setVisibility(View.VISIBLE);
        img_left.setOnClickListener(this);
        mtitle = (TextView) findViewById(R.id.eb_tv_title);
        mtitle.setVisibility(View.VISIBLE);
        mtitle.setText("添加律师费");
        mTvRight = (TextView) findViewById(eb_tv_right);
        mTvRight.setVisibility(View.VISIBLE);
        mTvRight.setText("添加");
        mTvRight.setOnClickListener(this);
    }

    @Override
    public void initData() {
        //滑动退出
//        addSwipeFinishLayout();
        mLawyerFeeInfoController = new LawyerFeeInfoController(this, this);
        mTimePicker = new TimePicker(this, 3,
                new TimePicker.OnPickerCallBack() {
                    @Override
                    public void pickerDone(String timeString, long timeInMillis) {
                        time.setText(timeString);
                    }
                });
    }

    List<LawyerFee> mBean = new ArrayList<LawyerFee>();

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.eb_tv_right://保存
                hintKbTwo();
                if (name.getText().toString().trim().length() == 0) {
                    showCToast("请输入款项名称");
                    return;
                }
                if (fee.getText().toString().trim().length() == 0) {
                    showCToast("请输入律师费");
                    return;
                }
//                if (clause.getText().toString().trim().length() == 0) {
//                    showCToast("请输入收款条件");
//                    return;
//                }
//                if (time.getText().toString().trim().length() == 0) {
//                    showCToast("请输选择收费时间");
//                    return;
//                }
                LawyerFee bean = new LawyerFee();
                bean.shouldCharge = fee.getText().toString().trim();
                String str = clause.getText().toString().trim().replaceAll("，", "★");
                bean.condition = clause.getText().toString().trim();
                bean.expectGatheringDate = time.getText().toString().trim();
                bean.paymentName = name.getText().toString().trim();
                mBean.add(bean);
                mAddLawyerFeeAdapter.notifyDataSetChanged();
                name.setText("");
                fee.setText("");
                clause.setText("");
                time.setText("");
//                Intent intent = new Intent();
//                intent.putExtra("entry_name", name.getText().toString().trim());
//                intent.putExtra("lawfee", fee.getText().toString().trim());
//                intent.putExtra("shoukuan_tiaojian", clause.getText().toString().trim());
//                intent.putExtra("shoufei_time", time.getText().toString().trim());
//                setResult(RESULT_OK, intent);
//                finish();
                break;
            case R.id.img_left:
                hintKbTwo();
                Intent intent = new Intent();
                LawyerFeeC b = new LawyerFeeC();
                b.bean = mBean;
                intent.putExtra("lawyerfee", b);
                setResult(RESULT_OK, intent);
                finish();
                break;
            case R.id.rl_fee:
                hintKbTwo();
                mTimePicker.show(getWindow().getDecorView());
                break;
            default:
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

    }

    @Override
    public void onFeeInfouccessed(LawyerFeeInfo beans) {
    }

    @Override
    public void onFeeInfoFailed() {

    }
}
